<?php
namespace app;
class Index
{
	protected $mongo = null;
	public function __construct()
	{
		$this->mongo = new \MongoDB\Driver\Manager('mongodb://127.0.0.1/');
	}
	
	public function index()
	{
		$bulk = new \MongoDB\Driver\BulkWrite();
		$doc = ['_id' => new \MongoDB\BSON\ObjectID(), 'name' => 'def'];
		$_id = $bulk->insert($doc);
		var_dump($_id);
		//$writeConcern = new \MongoDB\Driver\WriteConcern(\MongoDB\Driver\WriteConcern::MAJORITY, 1000);
		// dump($writeConcern->bsonSerialize());
		$writeResult = $this->mongo->executeBulkWrite('abc.runoob', $bulk);
		dump($writeResult->getInsertedCount());
		dump($writeResult->getServer()->getInfo());
	}
	
	public function query()
	{
//		$filter = ['name' => ['$type' => 'string']];
//        $filter = [
//            '$or' => [
//                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d37')],
//                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d38')],
//                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d39')]
//            ]
//        ];
//        $filter = ['index' => ['$lt' => 5]];
//        $filter = ['index' => ['$ne' => 5]];
//		$query = new \MongoDB\Driver\Query([], ['sort' => ['index' => -1]]);
		$query = new \MongoDB\Driver\Query([], ['sort' => ['index' => 1]]);
//		$query = new \MongoDB\Driver\Query($filter, ['sort' => ['index' => 1]]);
		$cursor = $this->mongo->executeQuery('abc.runoob', $query);
		dump($cursor->toArray());
	}

	public function update()
    {
//        $filter = ['name' => 'jdjd'];
        $filter = ['_id' => new \MongoDB\BSON\ObjectID('5c7e209d0ee76f2b48007d36')];
//        $doc = ['$set' => ['age'=>'128', 'homepage' => 'www.baidu.com', 'name' => '李彦宏']];
        $doc = [
            '$set' => [
                'about' => [
                    '$ref' => 'about',
                    '$id' => new \MongoDB\BSON\ObjectID('5c7e55660ee76f2b48007d3f'),
                    '$db' => 'abc'
                ]
            ]
        ];
        //$option = ['multi' => true, 'upsert' => false];
        $bulk = new \MongoDB\Driver\BulkWrite();
        $bulk->update($filter, $doc);
        $writeResult = $this->mongo->executeBulkWrite('abc.runoob', $bulk);
        dump($writeResult->getModifiedCount());
    }

    public function save()
    {
        $object_id = new \MongoDB\BSON\ObjectID('5c7e209c0ee76f2b48007d34');
        $bulk = new \MongoDB\Driver\BulkWrite();
        $bulk->delete(['_id' => $object_id]);
        $bulk->insert(['_id' => $object_id, 'age'=>'128', 'company' => '百度', 'name' => '李彦宏']);
        $writeResult = $this->mongo->executeBulkWrite('abc.runoob', $bulk);
        dump($writeResult->getDeletedCount());
        dump($writeResult->getInsertedCount());
    }

    public function delete()
    {
//        $filter = ['_id' => new \MongoDB\BSON\ObjectID('5c7df2450ee76f2b48007d33')];
        $filter = [
            '$or' => [
                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d37')],
                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d38')],
                ['_id' => new \MongoDB\BSON\ObjectID('5c7e209e0ee76f2b48007d39')]
            ]
        ];
//        $filter = ['name' => 'ok'];
        $bulk = new \MongoDB\Driver\BulkWrite();
        $bulk->delete($filter);
        $writeResult = $this->mongo->executeBulkWrite('abc.runoob', $bulk);
        dump($writeResult->getDeletedCount());
    }
}